package com.wwwarehouse.contract.query_express;

import android.os.Bundle;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.contract.bean.QueryExpressBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

import static com.alibaba.fastjson.JSON.parseObject;

/**
 * Created by clannad.shao on 2017/11/3.
 * @author clannad_sk
 * @date 2017/11/3
 */
@Route(path = "/contract/QueryExpressFragment")
public class QueryExpressFragment extends CommonBasePagerFragment {

    private static final int QUERY_EXPRESS_REQUEST_CODE = 0x44;
    private String contractId;

    @Override
    public void requestDatas() {
        super.requestDatas();
        Bundle bundle = getArguments();
        if(bundle != null){
            contractId = bundle.getString("contractId");
        }
        hideConfirmButton();
        Map<String, Object> map = new HashMap<>();
        map.put("contractId", contractId);
        loadData(ContractConstant.QUERY_EXPRESS_LIST, map, QUERY_EXPRESS_REQUEST_CODE);
    }

    /**
     * 请求成功回调
     * @param data
     * @param what
     */
    @Override
    public void onLoadSuccess(String data, int what) {
        if (what == QUERY_EXPRESS_REQUEST_CODE){
            QueryExpressBean queryExpress = parseObject(data, QueryExpressBean.class);
            //判断异常情况
            if(queryExpress == null){
                mStateLayout.showEmptyView(true);
                return;
            }
            if(queryExpress.getTrackList() == null || queryExpress.getTrackList().isEmpty()){
                showEmptyView(R.drawable.contract_no_datalist, R.string.query_express_no_datalist, false, true);
                return;
            }
            Bundle bundle = new Bundle();
            try{
                //需方业务单位名称
                bundle.putString("demanderName", queryExpress.getDemanderName());
                //需方业务单位ukid
                bundle.putString("demanderUkid", queryExpress.getDemanderUkid());
                //订单号
                bundle.putString("orderId", queryExpress.getOrderId());
                //下单时间
                bundle.putString("orderTime", queryExpress.getOrderTime());
                //总运单数
                bundle.putInt("packageNum", queryExpress.getPackageNum());
                //已签收运单及待签收
                bundle.putParcelable("packageTrackNum", queryExpress.getPackageTrackNum());
                //供方业务单位名称
                bundle.putString("supplierName", queryExpress.getSupplierName());
                //供方业务单位ukid
                bundle.putString("supplierUkid", queryExpress.getSupplierUkid());
                //供方头像
                bundle.putString("supplierIcon", queryExpress.getSupplierIcon());
                //数据源
                bundle.putParcelableArrayList("trackList", queryExpress.getTrackList());
                //parseJson And setView
                setData(queryExpress.getPackageNum(), 2, QueryExpressListItemFragment.class.getName()
                        , bundle, queryExpress.getTrackList());
            }catch (Exception e){
                e.printStackTrace();
                ToastUtils.showToast("数据源为空");
            }
        } else {
            //default

        }
    }

    /**
     * requestError
     * @param errorInfo
     */
    @Override
    public void onLoadError(String errorInfo) {
        //异常情况收集处理 TODO
        System.out.println(errorInfo);
    }

    /**
     * navigation 设置标题
     */
    @Override
    public void setTitle() {
        if (peekFragment() instanceof QueryExpressFragment) {
            mActivity.setTitle(getString(R.string.query_express));
        }
    }

    /**
     * 屏蔽下拉
     */
    @Override
    public void onDownPull() {
    }

    /**
     * stateLayout click to refresh
     */
    @Override
    public void onRefreshListener() {
        Map<String, Object> map = new HashMap<>();
        map.put("contractId", contractId);
        loadData(ContractConstant.QUERY_EXPRESS_LIST, map, QUERY_EXPRESS_REQUEST_CODE);
    }

    /**
     * destory
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
